package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignUpdaeModeConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity, Map<Long, Campaign> campaignMap,List<AreaList> areas) {
		boolean campaignUpdaeModeConverter = entity.getAction() == CampaignLog.Action.UPDATE_AUTO_TYPE;
		if (campaignUpdaeModeConverter) {
			if (StringUtils.isNotBlank(entity.getNote())) {
				ActionLogDto dto = new ActionLogDto();
				dto.from(entity);
				Campaign campaign = campaignMap.get(entity.getCampaignID());
				CampaignVo vo = this.unserializeCampaign(entity.getNote());
				if (campaign != null && vo != null && StringUtils.isNotBlank(vo.getOldAutoType())
						&& StringUtils.isNotBlank(vo.getNewAutoType())) {
					String[] params = { campaign.getTitle(), this.from(vo.getOldAutoType()),
							this.from(vo.getNewAutoType()) };
					dto.setReason(messageSource.getMessage("message.campaign.mode.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}
		return null;
	}

	private String from(String str) {
		if ("manual".equals(str)) {
			return "手动推广";
		}
		if ("auto".equals(str)) {
			return "低价引流";
		}
		if ("mix".equals(str)) {
			return "无线养分";
		}
		if ("focus".equals(str)) {
			return "重力主打";
		}
		if("semiauto".equals(str)){
			return "规则优化";
		}
		return null;
	}
}
